package com.infrastructure.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import com.infrastructure.net.RequestManager;

/**
 * Created by zyt on 16/6/13 20:38 .
 * Email : syy19930112@aliyun.com
 * Description:
 * <p/>
 * 封装与业务无关的公用逻辑.
 */
public abstract class BaseActivity extends AppCompatActivity {
    protected RequestManager requestManager;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initCommonVariables();
        initVariables();
        initViews(savedInstanceState);
        initData();
    }

    /**
     * 初始化公共变量
     */
    private void initCommonVariables() {
        requestManager = new RequestManager(this);
    }

    /**
     * 初始化变量,包括Intent带的数据和Activity内的变量
     */
    protected abstract void initVariables();

    /**
     * 加载layout布局文件,初始化控件,为控件挂上事件方法
     */
    protected abstract void initViews(Bundle savedInstanceState);

    /**
     * 调用MobileAPI获取数据
     */
    protected abstract void initData();


    @Override
    protected void onPause() {
        //在Activity暂停的同时设置停止请求,停止线程请求回调
        if (requestManager != null) {
            requestManager.cancelRequest();
        }
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        //在Activity销毁的同时设置停止请求,停止线程请求回调
        if (requestManager != null) {
            requestManager.cancelRequest();
        }
        super.onDestroy();
    }

    public RequestManager getRequestManager() {
        return requestManager;
    }
}
